import 'dart:io';

import 'package:flutter/material.dart';

void main() {
  runApp(new DialogPlugin());
}

/*
 * AboutDialog、AlertDialog、SimpleDialog、CupertinoAlertDialog、CupertinoFullscreenDialogTransition、BottomSheet ,自定义Dialog<br/>
 * https://blog.csdn.net/ruoshui_t/article/details/94318766
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
class DialogPlugin extends StatefulWidget {
  State<StatefulWidget> createState() {
    return new DialogPluginState();
  }
}

/*
 * 页面功能 <br/>
 */
class DialogPluginState extends State<StatefulWidget> {
  bool check = true;

  void initState() {
    super.initState();
  }

  Widget build(BuildContext context) {
    AppBar appBar = AppBar(
      leading: new IconButton(
        icon: new Icon(Icons.arrow_back),
        onPressed: () {},
      ),
      title: new Text('FlutterDesign'),
      centerTitle: true,
      actions: <Widget>[
        new Container(width: 0, height: 0),
        new Container(width: 0, height: 0),
      ],
      backgroundColor: Colors.blue,
    );

    return MaterialApp(
      home: new Scaffold(
        appBar: appBar,
        body: new Builder(builder: (BuildContext context) {
          return Column(
            children: <Widget>[
              RaisedButton(
                child: Text('dataPicker'),
                onPressed: () {
                  dataPicker(context);
                },
              ),
              RaisedButton(
                child: Text('timePicker'),
                onPressed: () {
                  timePicker(context);
                },
              ),
              RaisedButton(
                child: Text('dialog'),
                onPressed: () {
                  simpleDialog(context);
                },
              ),
            ],
          );
        }),
        backgroundColor: Colors.white,
        bottomNavigationBar: null,
      ),
    );
  }

  void dataPicker(context) {
    showDatePicker(
      context: context,
      initialDate: new DateTime.now(),
      firstDate: new DateTime.now().subtract(new Duration(days: 30)), // 减 30 天
      lastDate: new DateTime.now().add(new Duration(days: 30)), // 加 30 天
    ).then((DateTime val) {
      print(val); // 2018-07-12 00:00:00.000
    }).catchError((err) {
      print(err);
    });
  }

  void timePicker(context) {
    showTimePicker(
      context: context,
      initialTime: new TimeOfDay.now(),
    ).then((val) {
      print(val);
    }).catchError((err) {
      print(err);
    });
  }

  void alertDialog(context) {
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return new AlertDialog(
          title: new Text('标题'),
          content: new SingleChildScrollView(
            child: new ListBody(
              children: <Widget>[
                new Text('内容 1'),
                new Text('内容 2'),
              ],
            ),
          ),
          actions: <Widget>[
            new FlatButton(
              child: new Text('确定'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }

  void simpleDialog(context) {
    showDialog<Null>(
      context: context,
      builder: (BuildContext context) {
        return new SimpleDialog(
          title: new Text('选择'),
          children: <Widget>[
            new SimpleDialogOption(
              child: new Text('选项 1'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            new SimpleDialogOption(
              child: new Text('选项 2'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }
}
